package com.hl.payServer.controller;

import com.alibaba.fastjson.JSON;
import com.hl.payServer.model.PayProperty;
import com.hl.payServer.model.ResultModel;
import com.hl.payServer.model.ResultStatus;
import com.hl.payServer.service.PayPropertyService;
import com.hl.payServer.util.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.Column;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/property")
public class PropertyController {

    private static final Logger log = LoggerFactory.getLogger(PropertyController.class);
    @Autowired
    private PayPropertyService payPropertyService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResultModel list(@RequestParam("data") String data, HttpServletRequest request){
        PayProperty payProperty = null;
        try {
            payProperty = JSON.parseObject(data, PayProperty.class);
            Preconditions.checkNotNullAndEmpty(payProperty.getCompanyCode(), "companyCode");
            return ResultModel.ok(payPropertyService.queryList(payProperty));
        }catch (IllegalArgumentException illegalArgumentException){
            log.error("支付参数："+data+" 参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
            return ResultModel.error(ResultStatus.CLIENT_ERROR,"参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
        } catch (Exception e) {
            log.error("支付参数:"+data+"系统错误:error:"+e.getMessage(), e);
            return ResultModel.error(ResultStatus.SERVER_ERROR,"系统错误:error:"+e.getMessage());
        }
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResultModel save(@RequestParam("data") String data, HttpServletRequest request){
        PayProperty payProperty = null;
        try {
            payProperty = JSON.parseObject(data, PayProperty.class);
            Preconditions.checkNotNullAndEmpty(payProperty.getCompanyCode(), "companyCode");
            Preconditions.checkNotNullAndEmpty(payProperty.getCompanyName(), "companyName");
            Preconditions.checkNotNullAndEmpty(payProperty.getPayType(), "payType");
            Preconditions.checkPositive(payProperty.getPayChannle(), "payChannle");
            setPayName(payProperty);
            Preconditions.checkNotNullAndEmpty(payProperty.getAppId(), "appId");
            Preconditions.checkNotNullAndEmpty(payProperty.getPrivateKey(), "privateKey");
            if (payProperty.getPayChannle() == 1) {//支付宝必传
                Preconditions.checkNotNullAndEmpty(payProperty.getPublicKey(), "publicKey");
            } else {//微信必传
                Preconditions.checkNotNullAndEmpty(payProperty.getMchId(), "mchId");
            }
            payProperty.setIsDelete(0);
            payProperty.initCreateTime();
            return payPropertyService.save(payProperty);
        }catch (IllegalArgumentException illegalArgumentException){
            log.error("支付参数："+data+" 参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
            return ResultModel.error(ResultStatus.CLIENT_ERROR,"参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
        } catch (Exception e) {
            log.error("支付参数:"+data+"系统错误:error:"+e.getMessage(), e);
            return ResultModel.error(ResultStatus.SERVER_ERROR,"系统错误:error:"+e.getMessage());
        }
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultModel update(@RequestParam("data") String data, HttpServletRequest request){
        PayProperty payProperty = null;
        try {
            payProperty = JSON.parseObject(data, PayProperty.class);
            Preconditions.checkNotNullAndEmpty(payProperty.getId(), "id");
            if (payProperty.getPayChannle() != null) {
                Preconditions.checkPositive(payProperty.getPayChannle(), "payChannle");
                setPayName(payProperty);
                Preconditions.checkNotNullAndEmpty(payProperty.getAppId(), "appId");
                Preconditions.checkNotNullAndEmpty(payProperty.getPrivateKey(), "privateKey");
                if (payProperty.getPayChannle() == 1) {//支付宝必传
                    Preconditions.checkNotNullAndEmpty(payProperty.getPublicKey(), "publicKey");
                } else {//微信必传
                    Preconditions.checkNotNullAndEmpty(payProperty.getMchId(), "mchId");
                }
            }
            payProperty.setCreateTime(null);
            payProperty.initUpdateTime();
            return payPropertyService.update(payProperty);
        }catch (IllegalArgumentException illegalArgumentException){
            log.error("支付参数："+data+" 参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
            return ResultModel.error(ResultStatus.CLIENT_ERROR,"参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
        } catch (Exception e) {
            log.error("支付参数:"+data+"系统错误:error:"+e.getMessage(), e);
            return ResultModel.error(ResultStatus.SERVER_ERROR,"系统错误:error:"+e.getMessage());
        }
    }


    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResultModel delete(@RequestParam("id") String id, HttpServletRequest request){
        try {
            return payPropertyService.delete(id);
        } catch (Exception e) {
            log.error("支付参数:"+id+"系统错误:error:"+e.getMessage(), e);
            return ResultModel.error(ResultStatus.SERVER_ERROR,"系统错误:error:"+e.getMessage());
        }
    }

    public void setPayName(PayProperty payProperty){
        if (payProperty.getPayChannle() == 1) {
            payProperty.setPayName("支付宝");
        }else if (payProperty.getPayChannle() == 2) {
            payProperty.setPayName("微信");
        }else{
            throw new IllegalArgumentException("参数payChannle不签规则 1支付宝  2微信");
        }
    }
}
